﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Console_Manual_AIPro_HanNuo
{
    class Program
    {
        static Stack<int> leftColumn = new Stack<int>();
        static Stack<int> centerColumn = new Stack<int>();
        static Stack<int> rightColumn = new Stack<int>();

        static void Main(string[] args)
        {
            leftColumn.Push(3);
            leftColumn.Push(2);
            leftColumn.Push(1);

            var v = IsHanNuo(leftColumn);
        }

        #region Methods
        static void PrintHanNuo()
        {
            int[] counts = new int[] { leftColumn.Count, centerColumn.Count, rightColumn.Count };
            var useLeftColumn = leftColumn.ToList();
            var useCenterColumn = centerColumn.ToList();
            var useRightColumn = rightColumn.ToList();

            int maxValue = 0;
            for (int i = 0; i < (maxValue = counts.Max()); i++)
            {
                var v1 = i >= maxValue - useLeftColumn.Count ? useLeftColumn[i - maxValue + useLeftColumn.Count].ToString() : "|";
                var v2 = i >= maxValue - useCenterColumn.Count ? useCenterColumn[i - maxValue + useCenterColumn.Count].ToString() : "|";
                var v3 = i >= maxValue - useRightColumn.Count ? useRightColumn[i - maxValue + useRightColumn.Count].ToString() : "|";

                Console.WriteLine(string.Format("{0}\t{1}\t{2}", v1, v2, v3));
            }

            Console.ReadLine();
        }

        static void Move(string sourceCol, string targetCol)
        {
            int? sourceValue = null;
            switch (sourceCol)
            {
                case "1":
                    sourceValue = leftColumn.Pop();
                    break;
                case "2":
                    sourceValue = centerColumn.Pop();
                    break;
                case "3":
                    sourceValue = rightColumn.Pop();
                    break;
                default:
                    break;
            }

            switch (targetCol)
            {
                case "1":
                    leftColumn.Push(sourceValue.Value);
                    break;
                case "2":
                    centerColumn.Push(sourceValue.Value);
                    break;
                case "3":
                    rightColumn.Push(sourceValue.Value);
                    break;
                default:
                    break;
            }
        }

        static void RandomAI(Stack<int> leftColumn, Stack<int> centerColumn, Stack<int> rightColumn)
        {
            var allItems = leftColumn.Concat(centerColumn).Concat(rightColumn);
            var allMax = allItems.Max();
            var targetMax = rightColumn.Max();

            if (allMax == targetMax)
            {

            }
        }

        static int IsHanNuo(Stack<int> source)
        {
            if (source.Count == 0)
                return 0;
            else
            {
                var useSource = source.ToList();
                var preValue = useSource[0];

                for (int i = 1; i < useSource.Count - 1; i++)
                {
                    if (useSource[i] != preValue + 1)
                    {
                        return i;
                    }
                }

                return useSource.Count;
            }
        }

        #endregion
    }
}
